package com.aaa.mrcai.service.impl;


import com.aaa.mrcai.mapper.BusinessCheckMapper;
import com.aaa.mrcai.pojo.entity.*;
import com.aaa.mrcai.pojo.vo.CheckReasonVO;
import com.aaa.mrcai.query.BusinessCheckQuery;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.service.BusinessCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ Author: Lining
 * @ Date: 2021/12/5 18:12
 */
@Service
public class BusinessCheckServiceImpl implements BusinessCheckService {

    @Autowired
    private BusinessCheckMapper businessCheckMapper;

    @Autowired
    private HttpSession session;

    @Override
    public AjaxResult list(BusinessCheckQuery businessCheckQuery) {

        List<BusinessCheckInfo> list = businessCheckMapper.list(businessCheckQuery);
        System.out.println("service:"+businessCheckQuery);
        Long aLong = businessCheckMapper.queryBusinessTotalCount(businessCheckQuery);
        Map<String, Object> map = new HashMap<>();
        map.put("currentPageData",list);
        map.put("currentDataTotal",aLong);
        return AjaxResult.success(map);

    }


    @Override
    public AjaxResult stateList() {
        List<BusinessCheckState> businessCheckStates = businessCheckMapper.stateList();
        return  AjaxResult.success(businessCheckStates);
    }

    @Override
    public AjaxResult stateListOneTwo() {
        List<BusinessCheckState> businessCheckStates = businessCheckMapper.stateListOneTwo();
        return  AjaxResult.success(businessCheckStates);
    }

    @Override
    public AjaxResult areaList() {
        List<Area> areas = businessCheckMapper.areaList();
        return  AjaxResult.success(areas);
    }

    @Override
    public AjaxResult marketList() {
        List<Market> markets = businessCheckMapper.marketList();
        return  AjaxResult.success(markets);
    }

    @Override
    public AjaxResult getOne(int id) {
        BusinessCheckInfo one = businessCheckMapper.getOne(id);
        return AjaxResult.success(one);
    }

    @Override
    public AjaxResult updateState(CheckReasonVO checkReasonVO) {
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        checkReasonVO.setEnterdate(date.format(formatter));
        EmpInfo emp = (EmpInfo) session.getAttribute("emp");
        checkReasonVO.setChecker(emp.getEname());
        Long aLong = businessCheckMapper.updateState(checkReasonVO);
        if(aLong>0){
            return  AjaxResult.success();
        }
        return AjaxResult.defeated("修改失败");
    }


}
